/**
 * ReportGear(2011)
 */
package com.reportgear.designer.parameter.pane;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import com.reportgear.core.swing.layout.LayoutHelper;
import com.reportgear.core.swing.pane.base.EditorPane;
import com.reportgear.designer.editor.object.pane.ValueEditorPane;
import com.reportgear.designer.editor.object.pane.ValueEditorPaneFactory;
import com.reportgear.report.parameter.model.Parameter;

/**
 * 参数编辑面板
 * 
 * @version 1.0 2011-4-26
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class ParameterPane extends EditorPane<Parameter> {
	private static final long serialVersionUID = -2596937057366175751L;
	private JTextField nameTextField;
	private JTextField captionTextField;
	private ValueEditorPane valueEditor;
	private Parameter parameter;

	public ParameterPane() {
		initComponents();
	}

	private void initComponents() {
		JPanel editorPane = new JPanel(new GridBagLayout());

		this.nameTextField = new JTextField(10);
		this.nameTextField.setEditable(false);

		editorPane.add(new JLabel("名称", SwingConstants.LEFT), LayoutHelper.newGridBagConstraints(0, 0, 1, 1));
		editorPane.add(this.nameTextField, LayoutHelper.newGridBagConstraints(1, 0, 1, 1));

		this.captionTextField = new JTextField();
		editorPane.add(new JLabel("标签", SwingConstants.LEFT), LayoutHelper.newGridBagConstraints(0, 1, 1, 1));
		editorPane.add(this.captionTextField, LayoutHelper.newGridBagConstraints(1, 1, 1, 1));

		editorPane.add(new JLabel("值", SwingConstants.LEFT), LayoutHelper.newGridBagConstraints(0, 2, 1, 1));
		this.valueEditor = ValueEditorPaneFactory.createBasicValueEditorPane();
		editorPane.add(this.valueEditor, LayoutHelper.newGridBagConstraints(1, 2, 1, 1));

		this.setLayout(new BorderLayout());
		this.add(editorPane, BorderLayout.NORTH);
	}

	public void setNameEditable(boolean editable) {
		this.nameTextField.setEditable(editable);
	}

	@Override
	public void populate(Parameter parameter) {
		if (parameter == null) {
			return;
		}

		this.parameter = parameter;
		this.nameTextField.setText(parameter.getName());
		this.captionTextField.setText(parameter.getCaption());
		this.valueEditor.populate(parameter.getObject());
	}

	@Override
	public Parameter update() {
		if (this.parameter == null) {
			return null;
		}

		this.parameter.setName(this.nameTextField.getText());
		this.parameter.setCaption(this.captionTextField.getText().trim());
		this.parameter.setObject(this.valueEditor.update());

		return this.parameter;
	}

	@Override
	public int showDialog(Component owner) {
		Component c = owner;

		if (c == null) {
			c = this;
		}

		return this.showDialog(SwingUtilities.getWindowAncestor(c), "参数编辑", 400, 200);
	}
}